package display;
import java.util.ArrayList;

import data.Wave;

import simpleWave.PersonProcess;
import simpleWave.SynchronizedListener;
import simpleWave.Synchronizer;
import simpleWave.Verifier;

public class Display {
	
	private ArrayList<String> lines;

	private ArrayList<PersonProcess> persons;
	
	public Display(ArrayList<PersonProcess> persons, Synchronizer synchronizer) {
		this.persons = null;
		this.persons = persons;
		synchronizer.addSynchronizedListener(new DrawListener());
	}
	
	/*public void run() {
		while (true) {
			
			String line = "";
			for (Person person : persons) {
				//System.out.print(person);
				line += person + "";	
			}
			
			lines.add(line);
			
			System.out.print(line);
			
			// Verify
			ArrayList<Wave> waves = Verifier.verify(lines);
			for (Wave wave : waves) {
				System.out.print("    " + "wave: " + wave);
			}
			
			System.out.println("");
			
			//states.add(line);
			
			
			
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				System.out.println("Sleep failed");
				e.printStackTrace();
			}
		}
	}*/
	
	public void paint() {
		String line = "";
		for (PersonProcess person : persons) {
			//System.out.print(person);
			line += person + "";	
		}
		
		lines.add(line);
		
		System.out.print(line);
		
		// Verify
		ArrayList<Wave> waves = Verifier.verify(lines);
		for (Wave wave : waves) {
			System.out.print("    " + "wave: " + wave);
		}
		
		System.out.println("");
	}
	
	public ArrayList<String> getLines() {
		return lines;
	}

	public void setLines(ArrayList<String> lines) {
		this.lines = lines;
	}
	
	private class DrawListener implements SynchronizedListener {

		@Override
		public void synchronizationPerformed() {
			
			/*(new Thread () {
				public void run () {
					try {
						sleep(500);
						paint();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();*/
			
			paint();

		}
	}
}
